﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Win.Controls;
using DevComponents.Editors;

namespace PlanningGIS.Raster
{
    /// <summary>
    ///作用：DEM数据入库
    ///作者：盛琦
    ///编写日期：2013-09-05
    /// </summary>
    public partial class FrmDemImp :  Office2007Form
    {
        public FrmDemImp()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmDemImp_Load(object sender, EventArgs e)
        {
            txtScale.KeyPress += ControlVerify.txtIntBox_KeyPress;
            txtGrid.KeyPress += ControlVerify.txtDoubleBox_KeyPress;
            
            dateTimePicker1.Format = eDateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd";
            dateTimePicker1.Value = DateTime.Now;

        }

        private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "IMAGE(*.img)|*.img|TIF (*.tif)|*.tif";//"栅格文件(*.img,*.tif)|(*.img,*.tif)";
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtInput.Text = openFileDialog.FileName;

                string fileName = System.IO.Path.GetFileNameWithoutExtension(openFileDialog.FileName);
                txtName.Text = fileName;
            }
        }

        private void btnImp_Click(object sender, EventArgs e)
        {
            if (!RasterHelper.DbConfig.Context.ExistsObject("md_cq_dom"))
            {
                MsgHelper.ShowWarningMsgbox("数据库中缺少测区级DOM元数据表，请检查！");
                return;
            }

            if (txtInput.Text == "")
            {
                MsgHelper.ShowWarningMsgbox("请先选择需要入库的栅格数据！");
                return;
            }

            DemMetaData dem = new DemMetaData();
            dem.ID = Guid.NewGuid().ToString();


            string year = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString();

            if (month.Length == 1) month = "0" + month;

            string folder = RasterHelper.FTP_TYPE + @"\DEM\" + year + month;
            dem.Folder = folder;

            int? scale = null;
            if (txtScale.Text != "") scale = Int32.Parse(txtScale.Text);
            dem.Scale = scale;

            double? grid = null;
            if (txtGrid.Text != "") grid = Double.Parse(txtGrid.Text);
            dem.Grid = grid;

            dem.Name = txtName.Text;
            dem.Date = dateTimePicker1.Value;
            dem.DataSrc = txtSrc.Text;
            dem.Format = System.IO.Path.GetExtension(txtInput.Text);
            this.Cursor = Cursors.WaitCursor;

            try
            {
                RasterHelper.UploadDem(txtInput.Text, dem);

                MsgHelper.ShowInfomationMsgbox("DEM数据入库完成。");
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox("DEM数据入库失败：" + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
    }
}
